package com.lucaswangdev.biz.response.common;

import com.lucaswangdev.constant.common.PagingInfo;
import lombok.Data;

import java.util.List;

@Data
public class PagingResponse<T> {
    private boolean success;
    /**
     * 错误信息
     */
    private String errorMsg;

    private List<T> tableData;

    private PagingInfo paging;

    public static <T> PagingResponse<T> buildError(String errorMsg) {
        PagingResponse<T> response = new PagingResponse<>();
        response.setSuccess(false);
        response.setErrorMsg(errorMsg);
        return response;
    }

    public static <T> PagingResponse<T> build(List<T> tableData, long totalCount, Integer currentPage, Integer pageSize) {
        PagingResponse<T> response = new PagingResponse<>();
        response.setSuccess(true);
        response.setTableData(tableData);
        PagingInfo pagingInfo = new PagingInfo();
        pagingInfo.setCurrentPage(currentPage);
        pagingInfo.setPageSize(pageSize);
        pagingInfo.setTotalCount(Math.toIntExact(totalCount));
        response.setPaging(pagingInfo);
        return response;
    }

    public static <T> PagingResponse<T> build(List<T> tableData, PagingInfo pagingInfo) {
        PagingResponse<T> response = new PagingResponse<>();
        response.setSuccess(true);
        response.setTableData(tableData);
        PagingInfo paging = new PagingInfo();
        paging.setCurrentPage(pagingInfo.getCurrentPage());
        paging.setPageSize(pagingInfo.getPageSize());
        paging.setTotalCount(Math.toIntExact(pagingInfo.getTotalCount()));
        response.setPaging(paging);
        return response;
    }
}
